home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Range / Range.h < prev   
C/C++ Source or Header  |  1992-05-18  |  6KB  |  153 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. //
  13. // Created: MBN 08/21/89 -- Initial design and implementation
  14. // Updated: LGO 10/05/89 -- Make destructor inline
  15. // Updated: LGO 12/04/89 -- Efficiency re-write
  16. // Updated: DLS 03/27/91 -- New lite version
  17. //
  18. // The  parameterized  Range<Type,lbound,hbound> class is publicly derived from
  19. // the Range class  and supports arbitrary  user-defined ranges  for a type  of
  20. // object or built-in data type. This allows other higher level data structures
  21. // such as  the Vector and  List container classes  to  be parameterized over a
  22. // range of values for some  type so  that the programmer does  not have to add
  23. // bounds checking code to the application. A Vector  of positive integers, for
  24. // example, would  be easy  to declare, facilitating bounds checking restricted
  25. // to the code that implements the type, not the Vector.
  26. //
  27. // The  inclusive  upper and  lower   bounds for   the  range are  specified as
  28. // arguments  to the parameterized  type  declaration and  implementation macro
  29. // calls.   They are declared  as C++ constants  of  the  appropriate type.  No
  30. // storage is  allocated and all  references are compiled out by  the compiler.
  31. // Once  declared, a Range<Type,lbound,hbound> object cannot  have its upper or
  32. // lower bounds   changed,   since maintenance of all instances   would require
  33. // significant and unwarranted overhead.  Each Range<Type,lbound,hbound> object
  34. // has  a single private  data  slot that  holds ths instance value.  There are
  35. // three constructors.   The first is  a simple  empty  constructor. The second
  36. // accepts and sets an initial value.   The third  takes a reference to another
  37. // Range<Type,lbound,hbound> object and duplicates its value.
  38. //
  39. // All  Range<Type,lbound,hbound>  methods  are  implemented   as small  inline
  40. // functions to provide efficient encapsulation of objects,  including built-in
  41. // types such as int.  Methods are provided to get  the  lower and upper bounds
  42. // and set the value of the instance data  slot.  Assignment of  a value or one
  43. // object to another is supported by the overloaded operator= method.  Finally,
  44. // an implicit conversion  from a   Range<Type,lbound,hbound> object to a  Type
  45. // value is provided to allow mixed expressions.
  46. //
  47.  
  48. #ifndef RANGEH                    // If no Range class
  49. #define RANGEH
  50.  
  51. #ifndef BASE_RANGEH                // If no class definition
  52. #include <cool/Base_Range.h>        // Include header file
  53. #endif
  54.  
  55. // ensure no multiple definitions
  56. template <class Type, Type lbound, Type hbound> CoolRange {
  57.   typedef int (*Type##_CoolRange_Compare) (const Type&, const Type&);
  58. }
  59.  
  60. template <class Type, Type lbound, Type hbound>
  61. class CoolRange<Type,lbound,hbound> : public CoolRange {
  62. public:
  63.   CoolRange<Type,lbound,hbound> () {};        // Simple constructor
  64.   CoolRange<Type,lbound,hbound> (const Type& value); // Constructor with value
  65.   CoolRange<Type,lbound,hbound> (const CoolRange<Type,lbound,hbound>&); // Copy
  66.   ~CoolRange<Type,lbound,hbound> () {};              // Destructor
  67.   
  68.   inline const Type& low () const;        // Get low value range
  69.   inline const Type& high () const;        // Get high value range
  70.   inline void set (const Type&);        // Set instance value
  71.  
  72.   inline CoolRange<Type,lbound,hbound>&operator=(const CoolRange<Type,lbound,hbound>&);
  73.   inline operator Type () const;        // Implicit type conversion
  74.   inline void set_compare(Type##_CoolRange_Compare);// Set Range compare function
  75.  
  76. private:
  77.   Type data;                    // Storage for instance value
  78.   static Type low_bound;            // Class lower bound
  79.   static Type high_bound;            // Class upper bound
  80.   static Type##_CoolRange_Compare compare_s;    // Pointer operator== function
  81.  
  82.   char* value_string() const;            // convert data to string
  83.   void report_set_error () const;        // set method helper
  84.   void do_compare () const;            // set method helper
  85.   friend int Type##_CoolRange_compare_data (const Type&, const Type&);
  86. };
  87.  
  88. // Initialize lower/upper bounds
  89. template <class Type, Type lbound, Type hbound> CoolRange {
  90. Type CoolRange<Type,lbound,hbound>::low_bound = lbound;
  91. Type CoolRange<Type,lbound,hbound>::high_bound = hbound;
  92. }
  93.  
  94.  
  95. // low -- Return a reference to lower limit to allow user to set/get value
  96. // Input: None
  97. // Output: Reference to lower limit
  98.  
  99. template <class Type, Type lbound, Type hbound>
  100. inline const Type& CoolRange<Type,lbound,hbound>::low () const {
  101.   return this->low_bound;
  102. }
  103.  
  104.  
  105. // high -- Return a reference to upper limit to allow user to set/get value
  106. // Input:  None
  107. // Output: Reference to upper limit
  108.  
  109. template <class Type, Type lbound, Type hbound>
  110. inline const Type& CoolRange<Type,lbound,hbound>::high () const {
  111.   return this->high_bound;
  112. }
  113.  
  114.  
  115.  
  116. // set -- Set the value of the object
  117. // Input:  Value to set
  118. // Output: None
  119.  
  120. template <class Type, Type lbound, Type hbound>
  121. inline void CoolRange<Type,lbound,hbound>::set (const Type& value) {
  122.   this->data = value;
  123.   if (this->compare_s != NULL)            // When Custom compare function
  124.     this->do_compare();
  125.   else                         // Normal, fast case
  126.    // check both bounds at once to minimize the amount of inline code
  127.    if (value < this->low_bound || value > this->high_bound)
  128.      this->report_set_error ();            // Raise exception
  129. }
  130.  
  131.  
  132. // operator Type() -- Provide implicit conversion to what ever type this 
  133. //                    class is parameterized over to allow mixed expressions
  134. // Input:             None
  135. // Output:            Value of object
  136.  
  137. template <class Type, Type lbound, Type hbound> CoolRange {
  138. inline CoolRange<Type,lbound,hbound>::operator Type() const {return this->data;}
  139. }
  140.  
  141.  
  142. // set_compare() -- Set the compare function for the CoolRange class
  143. // Input:           Pointer to a compare function
  144. // Output:          None
  145.  
  146. template<class Type, Type lbound, Type hbound> 
  147. inline void CoolRange<Type,lbound,hbound>::set_compare (Type##_CoolRange_Compare c) {
  148.   this->compare_s = c;                // Set compare function
  149. }
  150.  
  151.  
  152. #endif                        // End #ifdef of RANGEH
  153.